package pl.kemot.impl.customer.add;

import pl.kemot.IPresenterComponentRemove;
import pl.kemot.customer.add.IPresenterCustomerAdd;
import pl.kemot.customer.add.IViewCustomerAdd;
import pl.kemot.customer.all.IViewCustomerAll;
import pl.kemot.impl.Context;
import pl.kemot.model.CustomerBO;
import pl.kemot.model.CustomerDTO;

public class PresenterCustomerAddImpl implements IPresenterCustomerAdd {

	private CustomerBO customerBO;
	private IViewCustomerAll viewCustomerAll;
	private IPresenterComponentRemove presenterComponentRemove;
	private IViewCustomerAdd viewCustomerAdd;

	public PresenterCustomerAddImpl() {
	}

	@Override
	public void setCustomerBO(CustomerBO customerBO) {
		this.customerBO = customerBO;
	}

	@Override
	public void setViewCustomerAll(IViewCustomerAll viewCustomerAll) {
		this.viewCustomerAll = viewCustomerAll;

	}

	@Override
	public void setPresenterComponentRemove(IPresenterComponentRemove presenterComponentRemove) {
		this.presenterComponentRemove = presenterComponentRemove;
	}

	@Override
	public void setViewCustomerAdd(IViewCustomerAdd viewCustomerAdd) {
		this.viewCustomerAdd = viewCustomerAdd;
	}

	@Override
	public void perform(Context context) {
		// pobieramy dane z formularza
		String customerFirstName = viewCustomerAdd.getFirstName();
		String customerLastName = viewCustomerAdd.getLastName();
		Integer customerAge = viewCustomerAdd.getAge();

		// tworzymy obiekt CustomerDTO
		CustomerDTO customerDTO = new CustomerDTO(customerFirstName, customerLastName, customerAge);

		// utrwalamy za pomocą utrwalacza
		customerDTO = customerBO.saveOrUpdate(customerDTO);

		// aktualizujemy spis klientów
		viewCustomerAll.addOrUpdateCustomer(customerDTO);

		// usuwamy formularz z UI
		context.put(IPresenterComponentRemove.OBJECT_TO_REMOVE, viewCustomerAdd);
		presenterComponentRemove.perform(context);
	}
}
